package com.ag.hmk.action;

import javax.annotation.Resource;

import com.ag.bean.TbAidgoodsku;
import com.ag.bean.TbAidgoogs;
import com.ag.bean.TbAidgoogsId;
import com.ag.bean.TbGoods;
import com.ag.bean.TbGoodsstatus;
import com.ag.bean.TbType;
import com.ag.common.BaseAction;
import com.ag.common.IDUtil;
import com.ag.common.IIDService;
import com.ag.hmk.service.impl.AidGoodsServImpl;
import com.ag.hmk.service.impl.AidGoodskuServImpl;
import com.ag.hmk.service.impl.GoodsServImpl;
import com.ag.hmk.service.impl.GoodsStatusServImpl;
import com.ag.hmk.service.impl.UnitServImpl;
import com.ag.hmk.service.impl.WukuTypeServImpl;

/**
 * 
 * @author hmk
 * 保存物资库，物资，物资库物资（中间表）
 */
public class SaveAidGoodsKUAction extends BaseAction {

	private static final long serialVersionUID = 8282033003426027091L;

	private AidGoodskuServImpl aidGoodskuServImpl;
	private WukuTypeServImpl wukuTypeServImpl;
	private UnitServImpl unitServImpl;
	private GoodsServImpl goodsServImpl;
	private GoodsStatusServImpl goodsStatusServImpl;
	private AidGoodsServImpl aidGoodsServImpl;
	@Resource(name = "iDServiceImpl")
	private IIDService idservice;
	
	private String typeID1;
	private String agkuName1;
	private String agkuUnit1;
	private String agkuScale1;
	private String agkuLocation1;
	private String agkuLat1;
	private String agkuLng1;
	
	private String agkuID1; //救援库ID
	private String gID1;
	private String gNum1;
	private String gstatusID1;
	
	private String ajaxRST;
	
	/*
	 * 保存救援库
	 */
	public String saveAGKU()throws Exception{
		this.setAjaxRST("0");
		try {
			TbType type2 = wukuTypeServImpl.findTypeByID(typeID1); //找出仓库类型
			if (type2!=null) {
				if (agkuName1!=null && agkuName1.length()>0 && 
					agkuUnit1!=null && agkuUnit1.length()>0 && 
					agkuLocation1!=null && agkuLocation1.length()>0) {
					
					TbAidgoodsku agku2 = new TbAidgoodsku();
					String aidID = idservice.findTablePK(IDUtil.TB_AIDGOODSKU);
					agku2.setAidId(aidID);
					agku2.setTbType(type2);
					agku2.setAidName(agkuName1);
					agku2.setAidUnit(agkuUnit1);
					agku2.setAidScale(agkuScale1);
					agku2.setAidLocation(agkuLocation1);
					agku2.setAidLat(agkuLat1);
					agku2.setAidLng(agkuLng1);
					
					aidGoodskuServImpl.save(agku2);       //保存物品仓库记录
					this.setAjaxRST("1");
					System.out.println("-----save aidgoodsku Ok");
				} else {
					System.out.println("---some ziduan can not to be null");
				}
			} else {
				System.out.println("---type can not to be null");
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.setAjaxRST("0");
		}
		return "saveAgkuOK";
	}
	
	
	/*
	 * 更新救援库
	 */
	public String updateAGKU()throws Exception{
		this.setAjaxRST("0");
		try {
			TbType type2 = wukuTypeServImpl.findTypeByID(typeID1); //找出仓库类型
			TbAidgoodsku agku2 = aidGoodskuServImpl.findByID(agkuID1);
			if (agku2!=null) {
				if (type2!=null) {
					if (agkuName1!=null && agkuName1.length()>0 && 
						agkuUnit1!=null && agkuUnit1.length()>0 && 
						agkuLocation1!=null && agkuLocation1.length()>0) {
						agku2.setTbType(type2);
						agku2.setAidName(agkuName1);
						agku2.setAidUnit(agkuUnit1);
						agku2.setAidScale(agkuScale1);
						agku2.setAidLocation(agkuLocation1);
						agku2.setAidLat(agkuLat1);
						agku2.setAidLng(agkuLng1);
						
						aidGoodskuServImpl.save(agku2);       //保存物品仓库记录
						this.setAjaxRST("1");
						System.out.println("-----save aidgoodsku Ok");
					} else {
						System.out.println("---some ziduan can not to be null");
					}
				} else {
					System.out.println("---type can not to be null");
				}
			} else {
				System.out.println("--------没有找到记录被修改");
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.setAjaxRST("0");
		}
		
		return "saveAgkuOK";
	}
	
	/*
	 * 新增物品，（为救援库新增一个物品，在中间表里插入一个记录）
	 */
	public String addGood()throws Exception{
		try {
			TbAidgoodsku agku2 = aidGoodskuServImpl.findByID(agkuID1);
			TbGoods good2 = goodsServImpl.findByID(gID1);
			TbGoodsstatus gstatus2 = goodsStatusServImpl.findByID(gstatusID1);
			if (agku2!=null && good2!=null && gstatus2!=null && gNum1!=null ) {
				TbAidgoogsId agId = new TbAidgoogsId();
				agId.setAidId(agkuID1);
				agId.setGoodsId(gID1);
				
				TbAidgoogs ag = new TbAidgoogs();
				ag.setId(agId);
				ag.setTbAidgoodsku(agku2);
				ag.setTbGoods(good2);
				ag.setTbGoodsstatus(gstatus2);
				ag.setAgNumber(Integer.parseInt(gNum1));
				aidGoodsServImpl.save(ag); //中间表插入一条记录
				this.setAjaxRST("1");
				
			} else {
				System.out.println("===字段为空====");
				this.setAjaxRST("0");
			}
		} catch (Exception e) {
			System.out.println("=====异常===");
			e.printStackTrace();
			this.setAjaxRST("0");
		}
		return "addMidGoodOK";
	}
	
	/*
	 * 更新物品，（改变中间表的数量和状态）
	 */
	public String updateGood()throws Exception{
		try {
			System.out.println("=========="+agkuID1+","+gID1+","+gstatusID1);
			TbGoodsstatus gstatus2 = goodsStatusServImpl.findByID(gstatusID1);
			TbAidgoogsId agId = new TbAidgoogsId();
			agId.setAidId(agkuID1);
			agId.setGoodsId(gID1);
			TbAidgoogs ag = aidGoodsServImpl.findByID(agId);
			if (ag!=null && gstatus2!=null && gNum1!=null ) {
				ag.setTbGoodsstatus(gstatus2);
				ag.setAgNumber(Integer.parseInt(gNum1));
				
				aidGoodsServImpl.save(ag); //中间表插入一条记录
				this.setAjaxRST("1");
			} else {
				System.out.println("===字段为空====");
				this.setAjaxRST("0");
			}
		} catch (Exception e) {
			System.out.println("=====异常===");
			e.printStackTrace();
			this.setAjaxRST("0");
		}
		
		return "addMidGoodOK";
	}
	
	/*
	 * 删除物资，（中间表）
	 */
	public String deleteGood()throws Exception{
		System.out.println("====deleteGood=====");
		try {
			TbAidgoogsId agId = new TbAidgoogsId();
			agId.setAidId(agkuID1);
			agId.setGoodsId(gID1);
			TbAidgoogs ag = aidGoodsServImpl.findByID(agId);
			if (ag!=null ) {
				aidGoodsServImpl.delete(ag); //中间表删除一条记录
				this.setAjaxRST("1");
				
			} else {
				System.out.println("===没记录====");
				this.setAjaxRST("0");
			}
		} catch (Exception e) {
			System.out.println("===deleteGood==异常===");
			e.printStackTrace();
			this.setAjaxRST("0");
		}
		
		return "addMidGoodOK";
	}

	public void setAidGoodskuServImpl(AidGoodskuServImpl aidGoodskuServImpl) {
		this.aidGoodskuServImpl = aidGoodskuServImpl;
	}

	public void setWukuTypeServImpl(WukuTypeServImpl wukuTypeServImpl) {
		this.wukuTypeServImpl = wukuTypeServImpl;
	}

	public void setTypeID1(String typeID1) {
		this.typeID1 = typeID1;
	}

	public void setAgkuName1(String agkuName1) {
		this.agkuName1 = agkuName1;
	}

	public void setAgkuUnit1(String agkuUnit1) {
		this.agkuUnit1 = agkuUnit1;
	}

	public void setAgkuScale1(String agkuScale1) {
		this.agkuScale1 = agkuScale1;
	}

	public void setAgkuLocation1(String agkuLocation1) {
		this.agkuLocation1 = agkuLocation1;
	}

	public void setAgkuLat1(String agkuLat1) {
		this.agkuLat1 = agkuLat1;
	}

	public void setAgkuLng1(String agkuLng1) {
		this.agkuLng1 = agkuLng1;
	}

	public String getAjaxRST() {
		return ajaxRST;
	}

	public void setAjaxRST(String ajaxRST) {
		this.ajaxRST = ajaxRST;
	}



	public UnitServImpl getUnitServImpl() {
		return unitServImpl;
	}

	public void setUnitServImpl(UnitServImpl unitServImpl) {
		this.unitServImpl = unitServImpl;
	}

	public void setGoodsServImpl(GoodsServImpl goodsServImpl) {
		this.goodsServImpl = goodsServImpl;
	}

	public void setGoodsStatusServImpl(GoodsStatusServImpl goodsStatusServImpl) {
		this.goodsStatusServImpl = goodsStatusServImpl;
	}

	public void setAidGoodsServImpl(AidGoodsServImpl aidGoodsServImpl) {
		this.aidGoodsServImpl = aidGoodsServImpl;
	}


	public void setAgkuID1(String agkuID1) {
		this.agkuID1 = agkuID1;
	}


	public void setGID1(String gid1) {
		gID1 = gid1;
	}


	public void setGNum1(String num1) {
		gNum1 = num1;
	}


	public void setGstatusID1(String gstatusID1) {
		this.gstatusID1 = gstatusID1;
	}
	
}
